<template>
	<view class="content">
		<view class="search-box">
			<!-- mSearch组件 如果使用原样式，删除组件元素-->
			<mSearch class="mSearch-input-box" :mode="2" button="inside" :placeholder="defaultKeyword"
				@search="doSearch(false)" @input="inputChange" @confirm="doSearch(false)" v-model="keyword"></mSearch>
			<!-- 原样式 如果使用原样式，恢复下方注销代码 -->
			<!-- 						
			<view class="input-box">
				<input type="text" :adjust-position="true" :placeholder="defaultKeyword" @input="inputChange" v-model="keyword" @confirm="doSearch(false)"
				 placeholder-class="placeholder-class" confirm-type="search">
			</view>
			<view class="search-btn" @tap="doSearch(false)">搜索</view> 
			 -->
			<!-- 原样式 end -->
		</view>
		<view class="search-keyword" v-if="showResult==false">
			<scroll-view class="keyword-list-box" v-show="isShowKeywordList" scroll-y>
				<block v-for="(row,index) in keywordList" :key="index">
					<view class="keyword-entry" hover-class="keyword-entry-tap">
						<view class="keyword-text" @tap.stop="doSearch(keywordList[index].keyword)">
							<rich-text :nodes="row.htmlStr"></rich-text>
						</view>
						<view class="keyword-img" @tap.stop="setKeyword(keywordList[index].keyword)">
							<image src="/static/HM-search/back.png"></image>
						</view>
					</view>
				</block>
			</scroll-view>
			<scroll-view class="keyword-box" scroll-y>
				<view class="keyword-block">
					<view class="keyword-list-header">
						<view style="width: 100%;justify-content: space-between;display: flex;">
							<view style="font-weight: 900;">历史搜索</view>
							<view>
								<image @tap="oldDelete"
									src="https://www.tdtye.com/files/vegetable_mall_file/default/delete.png"></image>
							</view>
						</view>
					</view>
					<view class="keyword">
						<view v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">
							{{keyword}}
						</view>
					</view>
				</view>
				<view class="keyword-block" v-if="hotShow">
					<view class="keyword-list-header">
						<view>热门搜索</view>
						<view>
							<image @tap="hotToggle" :src="'/static/HM-search/attention'+forbid+'.png'"></image>
						</view>
					</view>
					<view class="keyword" v-if="forbid==''">
						<view v-for="(keyword,index) in hotKeywordList" @tap="doSearch(keyword)" :key="index">
							{{keyword}}
						</view>
					</view>
					<view class="hide-hot-tis" v-else>
						<view>当前搜热门搜索已隐藏</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view style="padding: 30rpx;" v-if="resultList.length!=0">
			<u-list @scrolltolower="scrolltolower()" v-if="showResult" height="88vh">
				<u-list-item v-for="(item, index) in resultList">
					<view class="plotBox">
						<view style="padding: 25rpx 15rpx 0rpx 30rpx;display: flex;">
							<u--image :src="imgApi+item.plotAvatar" shape="circle" width="100rpx" height="100rpx">
							</u--image>
							<view class="title">
								<u--text :text="item.plotName" size="35rpx" bold block :lines="1"></u--text>
								<view style="line-height: 45rpx;margin-right: 10rpx;position: absolute;top: -10rpx;" v-if="$comm.checkNull(item.juli)">
									<u--text prefixIcon="map"
										iconStyle="font-size: 20rpx;color: #ff898b;line-height: 5px;"
										:text="'距离您'+item.juli+'km'" size="20rpx" color="#ff898b"></u--text>
								</view>
								<view class="tag" @click="goDetail(item.plotId,1)">
									<view style="line-height: 50rpx;">
										<u--text suffixIcon="play-right-fill"
											iconStyle="font-size: 20rpx;color: #fff;line-height: 5px;" text="去看看"
											size="20rpx" color="#fff"></u--text>
									</view>
								</view>
							</view>
						</view>
						<u-line margin="10rpx 0rpx"></u-line>
						<view class="vegetableBox" v-for="(vegetable, vegetableIndex) in item.vegetableList">
							<view class="rightBox" @click="perviewImg(imgApi+vegetable.avatar)">
								<u--image :src="imgApi+vegetable.avatar" width="100%" height="170rpx" radius="25rpx">
								</u--image>
							</view>
							<view class="leftBox" @click="goDetail(vegetable.id,2)">
								<u--text :text="vegetable.title" size="30rpx" block :lines="1"></u--text>
								<view>
									<view style="margin: 8rpx 0rpx;display: flex;">
										<u-tag text="采摘价" type="success" plain plainFill size="mini"></u-tag>
										<view style="margin-left: 10rpx;">
											<u--text :text="vegetable.price" size="30rpx" bold block mode="price"
												color="#7bcc58" v-if="vegetable.type==0||vegetable.type==2">
											</u--text>
											<u--text text="不可采摘" size="30rpx" bold block color="#409eff" v-else>
											</u--text>
										</view>
									</view>
									<view style="margin: 8rpx 0rpx;display: flex;">
										<u-tag text="种植价" type="primary" plain plainFill size="mini"></u-tag>
										<view style="margin-left: 10rpx;">
											<u--text :text="vegetable.growPrice" size="30rpx" bold block mode="price"
												color="#409eff" v-if="vegetable.type==1||vegetable.type==2">
											</u--text>
											<u--text text="不可种植" size="30rpx" bold block color="#409eff" v-else>
											</u--text>
										</view>
									</view>
								</view>
								<view style="display: flex;">
									<view class="tag" @click="goPlotDetail(item.plotId)">
										<view style="line-height: 50rpx;">
											<u--text suffixIcon="play-right-fill"
												iconStyle="font-size: 20rpx;color: #fff;line-height: 5px;" text="立即购买"
												size="20rpx" color="#fff"></u--text>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</u-list-item>
				<u-loadmore :status="status" />
			</u-list>
		</view>
		<view v-if="resultList.length==0&&showResult">
			<u-empty mode="search" icon="http://cdn.uviewui.com/uview/empty/search.png">
			</u-empty>
		</view>
		<!-- <u-list @scrolltolower="scrolltolower" showScrollbar="false" height="38vh" v-if="showResult">
			<u-list-item v-for="(item, index) in indexList" :key="index">
				<view class="vegetableBox">
					123
				</view>
			</u-list-item>
		</u-list> -->
	</view>
</template>

<script>
	//引用mSearch组件，如不需要删除即可
	import mSearch from '@/pageA/common/mehaotian-search-revision/mehaotian-search-revision.vue';
	export default {
		data() {
			return {
				imgApi: this.$reqConfig.URLIMAGE,
				indexList: [],
				urls: [
					'https://cdn.uviewui.com/uview/album/1.jpg',
					'https://cdn.uviewui.com/uview/album/2.jpg',
					'https://cdn.uviewui.com/uview/album/3.jpg',
					'https://cdn.uviewui.com/uview/album/4.jpg',
					'https://cdn.uviewui.com/uview/album/5.jpg',
					'https://cdn.uviewui.com/uview/album/6.jpg',
					'https://cdn.uviewui.com/uview/album/7.jpg',
					'https://cdn.uviewui.com/uview/album/8.jpg',
					'https://cdn.uviewui.com/uview/album/9.jpg',
					'https://cdn.uviewui.com/uview/album/10.jpg',
				],
				resultList: [],
				showResult: false,
				defaultKeyword: "",
				keyword: "",
				oldKeywordList: [],
				hotKeywordList: [],
				keywordList: [],
				forbid: '',
				isShowKeywordList: false,
				hotShow: false,
				ifBottomRefresh: true,
				status: "noMore",
				params: {
					pageNo: 1,
					pageSize: 5
				}
			}
		},
		onLoad() {
			this.init();
			// this.loadmore()
		},
		components: {
			//引用mSearch组件，如不需要删除即可
			mSearch
		},
		methods: {
			cleanList() {
				this.resultList = []
				this.params = {
					pageNo: 1,
					pageSize: 5
				}
				this.status = 'loading';
				this.showResult = false
				this.isShowKeywordList = false
				this.ifBottomRefresh = false
				this.$forceUpdate()
			},
			perviewImg(img) {
				uni.previewImage({
					urls: [img]
				})
			},
			scrolltolower() {
				if (this.status != 'noMore') {			
					this.status = 'loading';
					this.search()
				}
			},
			init() {
				this.loadDefaultKeyword();
				this.loadOldKeyword();
				this.loadHotKeyword();
			},
			blur() {
				uni.hideKeyboard()
			},
			//加载默认搜索关键字
			loadDefaultKeyword() {
				//定义默认搜索关键字，可以自己实现ajax请求数据再赋值,用户未输入时，以水印方式显示在输入框，直接不输入内容搜索会搜索默认关键字
				this.defaultKeyword = "输入关键字";
			},
			//加载历史搜索,自动读取本地Storage
			loadOldKeyword() {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						this.oldKeywordList = OldKeys;
					}
				});
			},
			//加载热门搜索
			loadHotKeyword() {
				//定义热门搜索关键字，可以自己实现ajax请求数据再赋值
				this.hotKeywordList = ['键盘', '鼠标', '显示器', '电脑主机', '蓝牙音箱', '笔记本电脑', '鼠标垫', 'USB', 'USB3.0'];
			},
			//监听输入
			inputChange(event) {
				if (!this.$comm.checkNull(event)) {
					this.cleanList()
					return;
				}
				//兼容引入组件时传入参数情况
				var keyword = event.detail ? event.detail.value : event;
				if (!keyword) {
					this.keywordList = [];
					this.isShowKeywordList = false;
					return;
				}
				this.isShowKeywordList = true;
				//以下示例截取淘宝的关键字，请替换成你的接口
				uni.request({
					url: 'https://suggest.taobao.com/sug?code=utf-8&q=' + keyword, //仅为示例
					success: (res) => {
						this.keywordList = [];
						this.keywordList = this.drawCorrelativeKeyword(res.data.result, keyword);
					}
				});
			},
			//高亮关键字
			drawCorrelativeKeyword(keywords, keyword) {
				var len = keywords.length,
					keywordArr = [];
				for (var i = 0; i < len; i++) {
					var row = keywords[i];
					//定义高亮#9f9f9f
					var html = row[0].replace(keyword, "<span style='color: #9f9f9f;'>" + keyword + "</span>");
					html = '<div>' + html + '</div>';
					var tmpObj = {
						keyword: row[0],
						htmlStr: html
					};
					keywordArr.push(tmpObj)
				}
				return keywordArr;
			},
			//顶置关键字
			setKeyword(index) {
				this.keyword = this.keywordList[index].keyword;
			},
			//清除历史搜索
			oldDelete() {
				uni.showModal({
					content: '确定清除历史搜索记录？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.oldKeywordList = [];
							uni.removeStorage({
								key: 'OldKeys'
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//热门搜索开关
			hotToggle() {
				this.forbid = this.forbid ? '' : '_forbid';
			},
			//执行搜索
			doSearch(keyword) {
				keyword = keyword === false ? this.keyword : keyword;
				this.keyword = keyword;
				if (!this.$comm.checkNull(this.keyword)) {
					uni.showToast({
						title: "请输入关键字搜索",
						icon: 'none',
						duration: 2000
					});
					return;
				}
				this.saveKeyword(keyword); //保存为历史  
				console.log("搜索执行了");
				this.cleanList()
				uni.showLoading({
					title: "搜索中...",
					icon: "none"
				})
				this.search()

				//以下是示例跳转淘宝搜索，可自己实现搜索逻辑
				/*
				//#ifdef APP-PLUS
				plus.runtime.openURL(encodeURI('taobao://s.taobao.com/search?q=' + keyword));
				//#endif
				//#ifdef H5
				window.location.href = 'taobao://s.taobao.com/search?q=' + keyword
				//#endif
				*/
			},
			search() {
				let dingwei = uni.getStorageSync("dingwei")
				this.$axios({
					url: "user/public/search",
					method: "post",
					data: {
						lng: dingwei.longitude,
						lat: dingwei.latitude,
						keyword: this.keyword,
						type: 1,
						pageNo: this.params.pageNo,
						pageSize: this.params.pageSize
					}
				}).then((res) => {
					if (res.result.pages >= this.params.pageNo) {
						// if (this.ifBottomRefresh) {
						// 	res.result.list.forEach(vegetable => {
						// 		let vegetableList = []
						// 		this.resultList.forEach(item => {
						// 			if (vegetable.plotId === item.plotId) {
						// 				item.vegetableList.push(vegetable);
						// 			} else {
						// 				vegetableList.push(vegetable)
						// 			}
						// 		})
						// 		if(vegetableList.length!=0){
						// 			this.resultList.push({
						// 				...vegetable,
						// 				vegetableList: vegetableList
						// 			})
						// 		}
						// 		console.log("vegetableList: ", vegetableList);
						// 		console.log("this.resultList: ", this.resultList);
						// 	})
						// } else {
						const recu = new Map()
						let list = res.result.list.filter((a) => !recu.has(a.plotId) && recu.set(a.plotId, 1))
						list.forEach(plot => {
							let vegetableList = []
							res.result.list.forEach(vegetable => {
								if (vegetable.plotId === plot.plotId) {
									vegetableList.push(vegetable)
								}
							})
							this.resultList.push({
								...plot,
								vegetableList: vegetableList
							})
						})
						// }
					} else {
						this.status = 'noMore'
					}
					if (this.params.pageNo >= res.result.pages) {
						this.status = 'noMore'
					}
					this.params.pageNo++;
					this.ifBottomRefresh = true
					this.showResult = true
					uni.hideLoading()
				})
			},

			//去重然后筛出土地列表
			plotVegetableList() {

			},
			goDetail(id, type) {
				if (type == 1) {
					uni.navigateTo({
						url: `/pageA/growVegetables/vegetablesList?id=${id}&type=0`
					})
				} else {
					uni.navigateTo({
						url: `/pageA/growVegetables/vegetablesDetails?id=${id}&type=0`
					})
				}
			},
			//保存关键字到历史记录
			saveKeyword(keyword) {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data);
						var findIndex = OldKeys.indexOf(keyword);
						if (findIndex == -1) {
							OldKeys.unshift(keyword);
						} else {
							OldKeys.splice(findIndex, 1);
							OldKeys.unshift(keyword);
						}
						//最多10个纪录
						OldKeys.length > 10 && OldKeys.pop();
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					},
					fail: (e) => {
						var OldKeys = [keyword];
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						});
						this.oldKeywordList = OldKeys; //更新历史搜索
					}
				});
			}
		}
	}
</script>
<style lang="scss">
	/* view {
		display: block;
	} */


	.plotBox {
		background-color: #fff;
		border-radius: 25rpx;
		margin: 10rpx 0rpx;

		.title {
			position: relative;
			margin-left: 20rpx;
			line-height: 90rpx;
			width: 80%;
			display: flex;

			.tag {
				width: 100rpx;
				margin-right: 15rpx;
				height: 45rpx;
				background-color: #ff9801;
				border-radius: 30rpx;
				display: flex;
				justify-content: center;
			}
		}

		.vegetableBox {
			display: flex;
			margin: 20rpx 25rpx;
			height: 200rpx;
			border-radius: 30rpx;

			.rightBox {
				padding: 15rpx 0rpx;
				width: 170px;
			}

			.leftBox {
				padding: 10rpx 5rpx;
				margin-left: 20rpx;
				width: 100%;

				.tag {
					width: 120rpx;
					height: 45rpx;
					background-color: #44bd32;
					display: flex;
					justify-content: center;
					margin-top: -30rpx;
					margin-left: auto;
				}
			}
		}
	}

	.search-box {
		width: 95%;
		background-color: rgb(242, 242, 242);
		padding: 15upx 2.5%;
		display: flex;
		justify-content: space-between;
		position: sticky;
		top: 0;
	}

	.search-box .mSearch-input-box {
		width: 100%;
	}

	.search-box .input-box {
		width: 85%;
		flex-shrink: 1;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.search-box .search-btn {
		width: 15%;
		margin: 0 0 0 2%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-shrink: 0;
		font-size: 28upx;
		color: #fff;
		background: linear-gradient(to right, #ff9801, #ff570a);
		border-radius: 60upx;
	}

	.search-box .input-box>input {
		width: 100%;
		height: 60upx;
		font-size: 32upx;
		border: 0;
		border-radius: 60upx;
		-webkit-appearance: none;
		-moz-appearance: none;
		appearance: none;
		padding: 0 3%;
		margin: 0;
		background-color: #ffffff;
	}

	.placeholder-class {
		color: #9e9e9e;
	}

	.search-keyword {
		width: 100%;
		background-color: rgb(242, 242, 242);
	}

	.keyword-list-box {
		height: calc(100vh - 110upx);
		padding-top: 10upx;
		border-radius: 20upx 20upx 0 0;
		background-color: #fff;
	}

	.keyword-entry-tap {
		background-color: #eee;
	}

	.keyword-entry {
		width: 94%;
		height: 80upx;
		margin: 0 3%;
		font-size: 30upx;
		color: #333;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: solid 1upx #e7e7e7;
	}

	.keyword-entry image {
		width: 60upx;
		height: 60upx;
	}

	.keyword-entry .keyword-text,
	.keyword-entry .keyword-img {
		height: 80upx;
		display: flex;
		align-items: center;
	}

	.keyword-entry .keyword-text {
		width: 90%;
	}

	.keyword-entry .keyword-img {
		width: 10%;
		justify-content: center;
	}

	.keyword-box {
		height: calc(100vh - 110upx);
		border-radius: 20upx 20upx 0 0;
		background-color: #fff;
	}

	.keyword-box .keyword-block {
		padding: 10upx 0;
	}

	.keyword-box .keyword-block .keyword-list-header {
		width: 94%;
		padding: 10upx 3%;
		font-size: 27upx;
		color: #333;
		display: flex;
		justify-content: space-between;
	}

	.keyword-box .keyword-block .keyword-list-header image {
		width: 40upx;
		height: 40upx;
	}

	.keyword-box .keyword-block .keyword {
		width: 94%;
		padding: 3px 3%;
		display: flex;
		flex-flow: wrap;
		justify-content: flex-start;
	}

	.keyword-box .keyword-block .hide-hot-tis {
		display: flex;
		justify-content: center;
		font-size: 28upx;
		color: #6b6b6b;
	}

	.keyword-box .keyword-block .keyword>view {
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 60upx;
		padding: 0 20upx;
		margin: 10upx 20upx 10upx 0;
		height: 60upx;
		font-size: 28upx;
		background-color: rgb(242, 242, 242);
		color: #6b6b6b;
	}
</style>
